package com.deli.demojdkproxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class JdkProxyFactory {
    private Object o;

    public JdkProxyFactory(Object o) {
        this.o = o;
    }

    public Object getProxyObject(){
        return Proxy.newProxyInstance(
                o.getClass().getClassLoader(),
                o.getClass().getInterfaces(), new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        if(method.getName().equals("save")){
                            writeLog();
                        }
                        Object invoke = method.invoke(o, args);
                        return invoke;
                    }
                });

    }
    private static void writeLog(){
        System.out.println("记录日志了~~~~~~~~~~~");
    }
}


